#include "customintvalidator.h"

CustomIntValidator::CustomIntValidator(QObject *parent)
    : QIntValidator{parent}
{

}

CustomIntValidator::CustomIntValidator(int bottom, int top, QObject *parent):
    QIntValidator(bottom, top, parent)
{

}

void CustomIntValidator::setRange(int bottom, int top)
{
    QIntValidator::setRange(bottom, top);
}

QValidator::State CustomIntValidator::validate(QString &s, int &n) const
{
    if (s.isEmpty())
    {
        return QValidator::Intermediate;
    }
    //这里判断超过位数了，变成无效的参数
    int intValue = s.toInt();
    if(intValue > top() || intValue < bottom())
        return QIntValidator::Invalid;
    return QIntValidator::validate(s, n);
}

void CustomIntValidator::fixup(QString &input) const
{
    int intValue = input.toInt();
    if(intValue < bottom())
        input = QString("%1").arg(bottom());
    if(intValue > top())
        input = QString("%1").arg(top());
}
